package com.hr.hrto.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hr.hrto.common.R;
import com.hr.hrto.domain.Category;
import com.hr.hrto.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *分类处理
 * @Author: 黄瑞
 * @Date: 2022/07/06/21:17
 * @Description:
 * @version:1.0
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
   @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
   public R<String> save(@RequestBody Category category){
       log.info("category:{}",category);
       categoryService.save(category);
      return R.success("新增分类成功");

   }
   @GetMapping("/page")
   public R<Page> page(int page,int pageSize){
        //分页构造器
        Page<Category> pageinfo =new Page<>(page,pageSize);
       //条件构造器
       LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
       //添加排除条件，根据sort进行排序
       queryWrapper.orderByAsc(Category::getSort);
       //进行分页查询
       categoryService.page(pageinfo, queryWrapper);
       return R.success(pageinfo);
   }
   @DeleteMapping()
   public R<String> delete(Long ids){//这里前端界面传输的参数是ids，所以要用IDS接受，也可改前端界面的数据为id
        log.info("删除分类，id为：{}",ids);
        categoryService.remove(ids);
        return R.success("已成功删除分类");
   }
   @PutMapping()
   public R<String> update(HttpServletRequest httpServletRequest,@RequestBody Category category){
        log.info("修改的信息：{}",category);
        categoryService.updateById(category);
        //更新人和更新时间已经在元数据对象处理器中处理了，所以不用写
        return R.success("修改信息分类成功");
   }
   @GetMapping("/list")
   public R<List<Category>> list(Category category){
        //条件构造器
       LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
       queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
       List<Category> list = categoryService.list(queryWrapper);
       return R.success(list);
   }
}
